<template>
  <div class="app-container">
    <git-navigation />
    <el-col :span="22" :offset="1">
      <el-divider />
      <el-row :gutter="10">
        <el-col :span="5" class="branch-col">全部分支</el-col>
        <el-col :span="4" :offset="8">
          <el-input
            v-model="search"
            placeholder="按分支名筛选"
            suffix-icon="el-icon-search"
            size="medium"
            @keyup.enter.native="getBranchList"
          />
        </el-col>
        <el-col :span="4">
          <el-button class="del-branch" size="medium" type @click="deleteMergedBranch">删除已合并的分支</el-button>
        </el-col>
        <el-col :span="1">
          <el-button size="medium" type="success" @click="addDialogVisible = true">新建分支</el-button>
        </el-col>
      </el-row>
      <el-divider />

      <el-table
        v-loading="listLoading"
        :data="branchList"
        :show-header="false"
        highlight-current-row
      >
        <el-table-column align="left">
          <template slot-scope="{row}">
            <i class="el-icon-s-operation" />
            <strong>{{ row.name }}</strong> &nbsp;
            <el-button v-if="row.default" type="primary" class="mini-button" size="mini">默认</el-button>
            <el-button v-if="row.protected" class="mini-button" size="mini" type="success">受保护</el-button>
            <el-button v-if="row.merged" class="mini-button" size="mini" type="success">已合并</el-button>
            <br>-o-
            <el-link
              :href="commitDetailUrl(row.commit.id)"
              :underline="false"
              type="primary"
            >{{ row.commit.short_id }}</el-link>
            <strong>·</strong>
            <el-link :underline="false" type="warning">{{ row.commit.committed_date | filterTime }}</el-link>
            <strong>·</strong>
            <el-link
              href="#"
              :underline="false"
              style="display: inline-block;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;width: 300px;"
              :title="row.commit.message"
            >{{ row.commit.message }}</el-link>
          </template>
        </el-table-column>
        <el-table-column align="right">
          <template slot-scope="{row, $index}">
            <el-dropdown split-button size="small">
              下载
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>
                  <el-link :underline="false" :href="row.download_url + '/.zip'">下载 zip</el-link>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-link :underline="false" :href="row.download_url + '/.tar.gz'">下载 tar.gz</el-link>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-link :underline="false" :href="row.download_url + '/.tar.bz2'">下载 tar.bz2</el-link>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-link :underline="false" :href="row.download_url + '/.tar'">下载 tar</el-link>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              :disabled="row.name === 'master'"
              @click="deleteBranch(row, $index)"
            />
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        title="新建分支"
        :visible.sync="addDialogVisible"
        width="60%"
        @open="getAllBranchAndTag"
      >
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item
            prop="branchName"
            label="分支名称"
            :rules="{
              required: true, message: '参数名不能为空', trigger: 'blur'
            }"
          >
            <el-input v-model="form.branchName" />
          </el-form-item>
          <el-form-item prop="from" label="创建自">
            <el-col>
              <el-select v-model="form.createFrom" style="width: 100%;" placeholder="placeholder">
                <el-option-group label="分支">
                  <el-option
                    v-for="item in allBranchAndTag.branches"
                    :key="item.name"
                    :label="item.name"
                    :value="item.name"
                  />
                </el-option-group>
                <el-option-group label="标签">
                  <el-option
                    v-for="item in allBranchAndTag.tags"
                    :key="item.name"
                    :label="item.name"
                    :value="item.name"
                  />
                </el-option-group>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="createBranch">创建分支</el-button>
            <el-button type @click="addDialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-col>
  </div>
</template>

<script>
import { getData, postData } from '@/api/requestData';
import { handleTimeDuration } from '@/utils/index';
import GitNavigation from './components/GitNavigation';

export default {
  components: {
    GitNavigation
  },
  filters: {
    filterTime(time) {
      return handleTimeDuration(time);
    }
  },
  data() {
    return {
      pid: this.$route.params.pid,
      pname: this.$route.params.projectName,
      // 按分支名搜索
      search: '',
      listLoading: true,
      branchList: [],
      allBranchAndTag: [],
      form: {
        branchName: '',
        createFrom: 'master'
      },
      addDialogVisible: false
    };
  },
  created() {
    this.getBranchList();
  },
  methods: {
    createBranch() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          postData('/git/create/branch/' + this.pid, this.form).then(
            (response) => {
              console.log(response);
              this.branchList.push(response.data);
              this.$message.success(response.message);
              this.addDialogVisible = false;
            }
          );
        }
      });
    },
    getBranchList() {
      this.listLoading = true;
      const name = this.search;
      getData('/git/public/all/branches/' + this.pid, { name: name })
        .then((response) => {
          this.branchList = response.data;
          this.listLoading = false;
        })
        .catch(() => {});
    },
    deleteBranch(row, index) {
      const confirmMsg = '删除 "' + row.name + '" 分支后将无法恢复, 您确定?';
      this.$confirm(confirmMsg, '提示', {
        confirmButtonType: 'warning',
        cancelButtonType: 'text',
        type: 'error'
      })
        .then(() => {
          const url =
            '/git/delete/branch/' + this.$route.params.pid + '/' + row.name;
          getData(url)
            .then((response) => {
              this.branchList.splice(index, 1);
              this.$message.success(response.message);
            })
            .catch(() => {});
        })
        .catch(() => {
          this.$message.info('取消删除');
        });
    },
    deleteMergedBranch() {
      const confirmMsg = '删除已合并的分支后将无法恢复, 您确定?';
      this.$confirm(confirmMsg, '提示', {
        confirmButtonType: 'warning',
        cancelButtonType: 'text',
        type: 'error'
      })
        .then(() => {
          const url = '/git/delete/merged/branch/' + this.pid;
          getData(url)
            .then((response) => {
              this.$message.success(response.message);
            })
            .catch(() => {});
        })
        .catch(() => {
          this.$message.info('取消删除');
        });
    },
    getAllBranchAndTag() {
      getData('/git/public/branch/tag/' + this.pid)
        .then((response) => {
          this.allBranchAndTag = response.data;
        })
        .catch(() => {});
    },
    commitDetailUrl(sha) {
      return '/git/commit/detail/' + this.pname + '/' + this.pid + '/' + sha;
    }
  }
};
</script>

<style scoped>
.mini-button {
  padding: 1px 2px;
}

.del-branch {
  color: red;
  border-color: red;
}

.branch-col {
  padding-top: 8px;
}
</style>
